package com.biyesheji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.biyesheji.domain.Orders;
import com.biyesheji.domain.User;
import com.biyesheji.dao.UserMapper;
import com.biyesheji.domain.Wares;
import com.biyesheji.service.IOrdersService;
import com.biyesheji.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户信息 服务实现类
 * </p>
 *
 * @author 崔
 * @since 2023-02-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Autowired
    IOrdersService ordersService;

    @Override
    public List<User> getUser(Long id, String phone, Integer status) {

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, User::getId, id);
        queryWrapper.like(phone != null && !phone.equals("-1"), User::getPhone, phone);
        queryWrapper.eq(status != null && status != 2, User::getStatus, status);

        List<User> userList = this.list(queryWrapper);
        return userList;
    }

    @Override
    public Boolean updateUser(User user) {

        this.updateById(user);

        if (user.getStatus() == 0){

            LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq( Orders::getUserPhone, user.getPhone());

            Orders orders = new Orders();
            orders.setStatus(0);
            ordersService.update(orders, queryWrapper);
        }


        return true;
    }
}
